/*
Creater ; TRUONGDV
Date : 06/05/2014
Idea : Tao ham khoi tao bien luu trong Cookie
Paras :
    + name  : Ten Cookie
    + value : Gia tri luu trong Cookie
    + expires : Thoi gian expires
    + path : Duong dan luu Cookie
    + domain : Ten mien
    + secure : Chinh sach
*/
function Set_Cookie( name, value, expires, path, domain, secure ){
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires ){
        expires = expires * 1000 * 60 * 60 * 24;
    }

    var expires_date = new Date( today.getTime() + (expires) );
    //
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}
/*
Creater ; TRUONGDV
Date : 06/05/2014
Idea : Tao ham khoi tao bien luu trong Cookie
Paras :
    + check_name  : Ten Cookie can lay gia tri
*/

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {

    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for ( i = 0; i < a_all_cookies.length; i++ ){
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );

        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if ( cookie_name == check_name ){
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 ) {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }

    //
    if ( !b_cookie_found ){
        return null;
    }
}
//---------------------------------------------------
//ham thuc hien khi nhap du lieu vao truong ngay thang
function DateOnkeyup(d,e){
    var pK = (e.which) ? e.which : window.event.keyCode;
    if (pK == 8) {
        return;
    }
    var dt = d.value;
    var da = dt.split('/');
    for (var a = 0; a < da.length; a++) {
        if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);
    }
    if (da[0] > 31) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
    if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
    if (da[2] > 9999) da[2] = da[2].substr(0,da[2].length-1);
    if(da[0] > 28 & da[1] == 02 & da[2]>999 ){
        if((da[2]%4)==0 & (da[2]% 100) ==0){
            da[0]=29;
        }
        else{
            da[0]=28;
        }
    }
    if(da[0] > 30 & (da[1] == 04|| da[1]==06 || da[1]==09 || da[1]==11 )){
        da[0]=30;
    }
    dt = da.join('/');
    if (dt.length == 2 || dt.length == 5) dt += '/';
    d.value = dt;
}
function removeClassName(elem, name){
    var remClass = elem.className;
    var re = new RegExp('(^| )' + name + '( |$)');
    remClass = remClass.replace(re, '$1');
    remClass = remClass.replace(/ $/, '');
    elem.className = remClass;
}
function removeEventOld(){
    if($('.auto-list').length >0){
        $('.auto-list').prev().hide();
        $('.auto-list').hide();
    }
}
/**
Create: Truongdv
Des: Load file Js va css
Params: arrUrl luu danh sach url {[]['type'],[]['url']}
*/
function loadfileJsCss(arrUrl){
    var count = arrUrl.length;
    for (var i = 0; i < count; i++){
        if(arrUrl[i]['type'] ==='js'){
            $('head').append('<script src="' + arrUrl[i]['src'] + '" type="text/javascript" charset="utf-8"></script>');
        }else{
            $('head').append('<link rel="stylesheet" type="text/css" href="' + arrUrl[i]['src'] + '">');
        }
    };
}

function _replaceBadChar(string){
    if(typeof(string) ==='undefined')
        return '';
    string = string.replace(/&/g,"&amp;");
    string = string.replace(/"/g,"&quot;");
    string = string.replace(/'/g,"&#39;");
    string = string.replace(/</g,"&lt;");
    string = string.replace(/>/g,"&gt;");
    return string;
}
function _restoreBadChar(string){
    if(typeof(string) ==='undefined')
        return '';
    string = string.replace(/&amp;/g,"&");
    string = string.replace(/&quot;/g,'"');
    string = string.replace(/&#39;/g,"'");
    string = string.replace(/&lt;/g,"<");
    string = string.replace(/&gt;/g,">");
    return string;
}
function _removeBadChar(string){
    if(typeof(string) ==='undefined')
        return '';
    string = string.replace(/&/g,"");
    string = string.replace(/"/g,"");
    string = string.replace(/'/g,"");
    string = string.replace(/</g,"");
    string = string.replace(/>/g,"");
    string = string.replace(/"/g,"");
    return string;
}

function createModalDialog(obj,functioncall){
    if($(obj).parent('[role="dialog"]').length ===0){
        functioncall();
    }else{
        $(obj).parent('[role="dialog"]').find(obj).remove();
        $(obj).parent('[role="dialog"]').remove();
        functioncall();
    }
}

function _replaceBadChar(string){
    if(typeof(string) ==='undefined')
        return '';
    string = string.replace(/&/g,"&amp;");
    string = string.replace(/"/g,"&quot;");
    string = string.replace(/'/g,"&#39;");
    string = string.replace(/</g,"&lt;");
    string = string.replace(/>/g,"&gt;");
    return string;
}
function _restoreBadChar(string){
    if(typeof(string) ==='undefined')
        return '';
    string = string.replace(/&amp;/g,"&");
    string = string.replace(/&quot;/g,'"');
    string = string.replace(/&#39;/g,"'");
    string = string.replace(/&lt;/g,"<");
    string = string.replace(/&gt;/g,">");
    return string;
}
function _removeBadChar(string){
    if(typeof(string) ==='undefined')
        return '';
    string = string.replace(/&/g,"");
    string = string.replace(/"/g,"");
    string = string.replace(/'/g,"");
    string = string.replace(/</g,"");
    string = string.replace(/>/g,"");
    string = string.replace(/"/g,"");
    return string;
}
function getDataXml(oForm){
    var dataXml = ''
        ,data=''
        ,data_store =''
        ,value= '';       
    var selects = $(oForm).find('[xml_data="true"]');
    selects.each(function(index){
        data_store  = $(this).attr('data_store');
        if ($.inArray($(this).attr('type'), ['checkbox','radio']) !== -1) {
            value = ($(this).is(':checked') ? $(this).val() : '');
        }else {
            value = $(this).val();
        };
        data += _replaceBadChar(value) + ',';

        if(index == selects.length - 1 || selects.eq(index + 1).attr('data_store') != data_store) {
            // select the next span
            data = data.substring(0,data.length -1);
            dataXml += '<'+data_store+'>'+ data +'</'+data_store+'>';
            data = '';
        }
    });
    if(dataXml)
        dataXml = '<data_list>'+ dataXml +'</data_list>';
    return dataXml;
}
function lddadepicker(objParrent){
    if (typeof(objParrent) ==='undefined') {
        objParrent = $('body');
    }
    $(objParrent).find('input[date="isdate"]').each(function(){
        $(this).datepicker({
            changeMonth: true,
            gotoCurrent: true,
            minDate: new Date(2010, 1 - 1, 1),
            changeYear: true
        });
    })
}
function setAttachEven(objParrent,ajaxurl){
    if (typeof(objParrent) ==='undefined') {
        objParrent = $('body');
    }
    $(objParrent).find('span.itemfile').each(function(){
		 $(this).unbind('click').click(function(){
				var value = $(this).attr('value');
				openAttach(value,ajaxurl);
		 });
    })
}
function openAttach(value,ajaxurl){
	var url = ajaxurl+'item/'+value;
	window.open(url);
}